﻿using Demo.Util;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.Service
{
    internal class UserService
    {
        public void Add(User user)
        {
            SqlUtil.GetDb().Insertable(user).ExecuteCommand();
        }

        public User Get(int id)
        {
            return SqlUtil.GetDb().Queryable<User>().First(it=>it.Id == id);
        }

        public void Update(User user)
        {
            SqlUtil.GetDb().Updateable(user).ExecuteCommand();
        }

        public void Delete(int id)
        {
            SqlUtil.GetDb().Deleteable<User>().Where(it => it.Id == id).ExecuteCommand();
        }

        public List<User> List(string searchKeyword)
        {
            if (string.IsNullOrEmpty(searchKeyword))
            {
                return SqlUtil.GetDb().Queryable<User>().ToList();
            }
            return SqlUtil.GetDb().Queryable<User>().Where(it => it.Name.Contains(searchKeyword)).ToList();
        }

        public List<User> Page(string searchKeyword,int pageNubmer,int pageSize)
        {
            if (string.IsNullOrEmpty(searchKeyword))
            {
                return SqlUtil.GetDb().Queryable<User>().ToPageList(pageNubmer, pageSize);
            }
            return SqlUtil.GetDb().Queryable<User>().Where(it => it.Name.Contains(searchKeyword)).ToPageList(pageNubmer, pageSize);
        }
    }
}
